<?php

namespace app\home\controller;

use app\common\model\Goods;
use think\Db;

class Index extends Common
{
    
    public $user;
    
    public function initialize()
    {
        parent::initialize();
    }
    
    public function index(Goods $goods)
    {
        
        if (!session('uid')) {
            header('location:' . url('/login'));
        }
        $list1 = $goods->limit(16)->order('id desc')->select();
        $list2 = $goods->limit(8)->order('id asc')->select();
        $this->assign('list1', $list1);
        $this->assign('list2', $list2);
        return $this->fetch();
    }
    
    public function login()
    {
        
        return $this->fetch();
    }
    
    //登录
    public function doLogin()
    {
        $name = input('post.username');
        $pwd = input('post.password');
        $user = Db::name('users')->where('username', $name)->find();
        if ($user) {
            if ($user['status'] == 1 && $user['password'] == md5($pwd)) {
                session('username', $user['username']);
                session('uid', $user['id']);
                $this->success('Login success', Url('home/index/index'));
            } else {
                $this->error('Account password error');
            }
        } else {
            $this->error('Account password error');
        }
    }
    
}